package com.example.horizontalgridview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private static final String TAG = "MainActivity";
	private static final int GRIDVIEW_COUNT = 4;
	private Button button;
	private List<Integer> lists = new ArrayList<Integer>();
	private List<MyAdapter> mGridViewAdapters = new ArrayList<MainActivity.MyAdapter>();
	private ViewPager mViewPager;
	private List<View> mAllViews = new ArrayList<View>();
	private MyViewPagerAdapter myViewPagerAdapter;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.langsi_popup_layout);
		mContext = getApplicationContext();
		button = (Button) findViewById(R.id.langsi_popup_cancel);
		button.setOnClickListener(this);
		for (int i = 0; i < 23; i++) {
			lists.add(i);
		}

		mViewPager = (ViewPager) findViewById(R.id.langsi_popup_viewpager);
		loadViews();
	}


	private void loadViews() {
		int pageCount = (lists.size() + GRIDVIEW_COUNT - 1) / GRIDVIEW_COUNT;
		Log.d(TAG, "pageCount: " + pageCount);
		mGridViewAdapters.clear();
		mAllViews.clear();
		LayoutInflater inflater = LayoutInflater.from(this);
		for (int i = 0; i < pageCount; i++) {
			View mView = inflater.inflate(R.layout.langsi_popup_gridview, null);
			GridView mGridView = (GridView) mView
					.findViewById(R.id.langsi_popup_gridview);
			MyAdapter adapter = new MyAdapter(mContext, i);
			mGridView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			mGridViewAdapters.add(adapter);
			mAllViews.add(mView);
		}

		myViewPagerAdapter = new MyViewPagerAdapter();
		mViewPager.setAdapter(myViewPagerAdapter);
		myViewPagerAdapter.notifyDataSetChanged();
	}

	private class MyViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mAllViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			mViewPager.removeView((View) object);
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(mAllViews.get(position),
					new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
			return mAllViews.get(position);
		}
	}

	private class MyAdapter extends BaseAdapter {

		private Context mContext;
		private int pagePosition;
		private LayoutInflater mInflater;

		private MyAdapter(Context context, int pagePosition) {
			this.mContext = context;
			this.pagePosition = pagePosition;
			mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			int size = (lists == null ? 0 : lists.size() - GRIDVIEW_COUNT
					* pagePosition);
			Log.d(TAG, "size: " + size);
			if (size > GRIDVIEW_COUNT) {
				return GRIDVIEW_COUNT;
			} else {
				return size > 0 ? size : 0;
			}
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			int nowPosition = GRIDVIEW_COUNT * pagePosition + position;
			ViewHolder holder;
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = mInflater.inflate(R.layout.langsi_popup_item,null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.langsi_popup_app_name);
				holder.icon = (ImageView) convertView.findViewById(R.id.langsi_popup_app_icon);
				holder.layout = (RelativeLayout) convertView.findViewById(R.id.langsi_popup_item_layout);
				convertView.setTag(holder);
			}
			holder.layout.setTag(nowPosition);
			holder.layout.setOnClickListener(MainActivity.this);
			holder.name.setText("手机_" + nowPosition);
			return convertView;
		}
	}

	static class ViewHolder {
		RelativeLayout layout;
		ImageView icon;
		TextView name;
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.langsi_popup_cancel:
			finish();
			break;
		case R.id.langsi_popup_item_layout:
			int id = (Integer) v.getTag();
			Toast.makeText(mContext, "position> " + id, Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
	}
	
}
